package com.asen.commons.core.json;

import com.asen.commons.exception.IllegalLogicException;
import com.asen.commons.utils.DateUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class MultiDateDeserializer extends DateDeserializers.DateDeserializer {
    private static final long serialVersionUID = -4702933060688923725L;

    public MultiDateDeserializer() {
    }

    public MultiDateDeserializer(DateDeserializers.DateDeserializer base, DateFormat df, String formatString) {
        super(base, df, formatString);
    }

    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = (JsonNode)p.getCodec().readTree(p);
        String date = node.textValue();
        if (StringUtils.isBlank(date)) {
            return null;
        } else {
            try {
                return date.contains("T") && date.contains("+") ? (new StdDateFormat()).parse(date) : DateUtils.parseDate(date, DateUtil.ALL_FORMATS);
            } catch (ParseException var6) {
                throw new IllegalLogicException("Unable to parse the date: " + date);
            }
        }
    }

    protected DateDeserializers.DateDeserializer withDateFormat(DateFormat df, String formatString) {
        return new MultiDateDeserializer(this, df, formatString);
    }
}
